var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var body = document.getElementsByTagName('body')[0];
var finalScore = 10000;
var mousedown = false;
var oRatate = 0;
var aRotate = 0;
var oNum = 6;
var scoreArr = [];
var countArr = [];
var reverse = 0;
//按下之后获取鼠标
var getFishBeDrop = {
  a: 0.1,
  b: 0.3,
  c: 0.6,
  d: 0.9,
  e: 1.2,
  f: 1.5,
  g: 1.8,
  h: 2.1,
  i: 2.3,
  j: 2.6,
  k: 2.9,
  l: 3.1
}


canvas.onmousedown = function(e) {
  var e = e || window.event;
  mousedown = true;
  var a = getAngle(Cannon.x, Cannon.y, parseInt(e.pageX - canvas.offsetLeft), parseInt(e.pageY - canvas.offsetTop));
  var bullet = createBullet(Cannon.x + Cannon.w / 2 - 40, Cannon.y + 10);
  bullet.rotate = a - Math.PI / 2;
  sprites.push(bullet);
  var x1 = e.pageX - canvas.offsetLeft;
  var y1 = e.pageY - canvas.offsetTop;
  if (x1 < 478 && x1 > 447 && y1 < 760 && y1 > 734) {
    oNum--;
    if (oNum < 0) {
      oNum = 6;
    }
  }
  //右侧按钮 557 591  671 695
  if (x1 < 597 && x1 > 566 && y1 < 760 && y1 > 734) {
    oNum++;
    if (oNum > 6) {
      oNum = 0;
    }
  }
  finalScore -= parseInt((10 * ControlNum(oNum)));
}

function ControlNum(a) {
  if (a == 0) {
    a = 7
  }
  return a
}
canvas.onmousemove = function(e) {
  var e = e || window.event;
  oRatate = getAngle(Cannon.x, Cannon.y, parseInt(e.pageX - canvas.offsetLeft), parseInt(e.pageY - canvas.offsetTop));
  Cannon.rotate = oRatate;
  // console.log(oRatate);
}
document.onmouseup = function() {
  mousedown = false;
}
var time = 0;
var lastBulletTime = 0;
var sprites = [];
var fishsArr = [];
var webArr = [];
var AnotherFish = [];
var aFishCrash = 0;
var bFishCrash = 0;
testAnmation();

function testAnmation() {
  var num = random(1, 13);
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  Bg.draw();

  for (var i in sprites) {
    sprites[i].draw();
    sprites[i].move();
  }

  if (time % 40 == 0) {
    if (appear(random(0, 100)) == 0) {
      var fish = createFish(1);
      fish.get = getFishBeDrop.a;

      fishsArr.push(fish);
    }
    if (appear(random(0, 100)) == 1) {
      var fish = createFish(2);
      fish.get = getFishBeDrop.b;
      fishsArr.push(fish);
    }
    if (appear(random(0, 100)) == 2) {
      var fish = createFish(3);
      fish.get = getFishBeDrop.c;
      fishsArr.push(fish);
    }
    if (appear(random(0, 100)) == 3) {
      var fish = createFish(4);
      fish.get = getFishBeDrop.d;
      fishsArr.push(fish);
    }
    if (appear(random(0, 100)) == 4) {
      var fish = createFish(5);
      fish.get = getFishBeDrop.e;
      fishsArr.push(fish);
    }
    if (appear(random(0, 100)) == 5) {
      var fish = createFish(6);
      fish.get = getFishBeDrop.f;
      AnotherFish.push(fish);
    }
    if (appear(random(0, 100)) == 6) {
      var fish = createFish(7);
      fish.get = getFishBeDrop.g;
      AnotherFish.push(fish);
    }
    if (appear(random(0, 100)) == 7) {
      var fish = createFish(8);
      fish.get = getFishBeDrop.h;
      AnotherFish.push(fish);
    }
    if (appear(random(0, 100)) == 8) {
      var fish = createFish(9);
      fish.get = getFishBeDrop.i;
      AnotherFish.push(fish);
    }
    if (appear(random(0, 100)) == 9) {
      var fish = createFish(10);
      fish.get = getFishBeDrop.j;
      AnotherFish.push(fish);
    }
    if (appear(random(0, 100)) == 10) {
      var fish = createFish(11);
      fish.get = getFishBeDrop.k;
      AnotherFish.push(fish);
    }
    if (appear(random(0, 100)) == 11) {
      var fish = createFish(12);
      fish.get = getFishBeDrop.l;
      AnotherFish.push(fish);
    }
  }


  for (i = 0; i < sprites.length; i++) {
    var a = sprites[i].crashX + 5;
    var b = sprites[i].crashY;
    for (j = 0; j < fishsArr.length; j++) {
      if (sprites[i] && fishsArr[j]) {
        var a1 = fishsArr[j].crashX;
        var b1 = fishsArr[j].crashY;
        var a2 = sprites[i].crashX - fishsArr[j].crashX;
        var b2 = sprites[i].crashY - fishsArr[j].crashY;
        var c1 = Math.sqrt(a2 * a2 + b2 * b2);
        var r = fishsArr[j].cells[0].w / 2 + sprites[i].width / 2;
        // 检测碰撞
        var obj = {};
        if (c1 <= r) {
          obj.x = sprites[i].left;
          obj.y = sprites[i].top;
          obj.Cx = sprites[i].Cx;
          obj.rotate = sprites[i].rotate;
          obj.url = oNum;
          var web = createWeb(obj);
          webArr.push(web);
          aFishCrash++;
          fishsArr[j].flag = false;
          if (randomGetFish(aFishCrash, fishsArr[j].get)) {
            var obj = {};
            obj.w = fishsArr[j].crashX;
            obj.h = fishsArr[j].crashY;
            /***********鱼的分数*************/
            if (fishsArr[j].get == getFishBeDrop.a) {
              var countScore = createCount(1, obj);
              finalScore += 100;
            }
            if (fishsArr[j].get == getFishBeDrop.b) {
              var countScore = createCount(2, obj);
              finalScore += 200;
            }
            if (fishsArr[j].get == getFishBeDrop.c) {
              var countScore = createCount(2, obj);
              finalScore += 200;
            }
            if (fishsArr[j].get == getFishBeDrop.d) {
              var countScore = createCount(3, obj);
              finalScore += 300;
            }
            if (fishsArr[j].get == getFishBeDrop.e) {
              var countScore = createCount(4, obj);
              finalScore += 400;
            }
            if (fishsArr[j].get == getFishBeDrop.f) {
              var countScore = createCount(5, obj);
              finalScore += 500;
            }
            if (fishsArr[j].get == getFishBeDrop.f) {
              var countScore = createCount(6, obj);
              finalScore += 600;
            }
            countArr.push(countScore);
            fishsArr.splice(j, 1);
            aFishCrash = 0;
          }
          sprites.splice(i, 1);
        }
      }
    }
  }
  for (i = 0; i < sprites.length; i++) {
    var a = sprites[i].crashX + 5;
    var b = sprites[i].crashY;
    // var c=Math.sqrt(a*a+b*b);
    for (j = 0; j < AnotherFish.length; j++) {
      if (sprites[i] && AnotherFish[j]) {
        var a1 = AnotherFish[j].crashX;
        var b1 = AnotherFish[j].crashY;
        var a2 = sprites[i].crashX - AnotherFish[j].crashX;
        var b2 = sprites[i].crashY - AnotherFish[j].crashY;
        var c1 = Math.sqrt(a2 * a2 + b2 * b2);
        var r = AnotherFish[j].cells[0].w / 2 + sprites[i].width / 2;
        // 检测碰撞
        var obj = {};
        if (c1 <= r) {
          var appearTime = 0;
          obj.x = sprites[i].left;
          obj.y = sprites[i].top;
          obj.Cx = sprites[i].Cx;
          obj.rotate = sprites[i].rotate;
          obj.url = oNum;
          var web = createWeb(obj);
          webArr.push(web);
          bFishCrash++;
          AnotherFish[j].flag = false;
          if (randomGetFish(bFishCrash, AnotherFish[j].get)) {
            var obj = {};
            obj.w = AnotherFish[j].crashX;
            obj.h = AnotherFish[j].crashY;
            if (AnotherFish[j].get == getFishBeDrop.g) {
              var countScore = createCount(6, obj);
              finalScore += 600;
            }
            if (AnotherFish[j].get == getFishBeDrop.h) {
              var countScore = createCount(7, obj);
              finalScore += 700;
            }
            if (AnotherFish[j].get == getFishBeDrop.i) {
              var countScore = createCount(8, obj);
              finalScore += 800;
            }
            if (AnotherFish[j].get == getFishBeDrop.j) {
              var countScore = createCount(9, obj);
              finalScore += 900;
            }
            if (AnotherFish[j].get == getFishBeDrop.k) {
              var score = createScore(1, obj);
              finalScore += 1000;
            }
            if (AnotherFish[j].get == getFishBeDrop.l) {
              var score = createScore(2, obj);
              finalScore += 1200;
            }
            scoreArr.push(score);
            countArr.push(countScore);
            AnotherFish.splice(j, 1);

            bFishCrash = 0;
          }
          sprites.splice(i, 1);
        }
      }
    }
  }
  for (var i in webArr) {
    webArr[i].draw();
    webArr[i].update();
  }
  for (var i in scoreArr) {
    if (scoreArr[i]) {
      scoreArr[i].draw();
      scoreArr[i].update();
      if (scoreArr[i].change > 100) {
        scoreArr.splice(i, 1);
      }
    }
  }
  for (var i in countArr) {
    if (countArr[i]) {
      countArr[i].draw();
      countArr[i].update();
      if (countArr[i].change > 100) {
        countArr.splice(i, 1);
      }
    }
  }
  for (var i in webArr) {
    webArr[i].update();
    if (webArr[i].scale >= 1) {
      webArr.splice(i, 1);
    }
  }
  for (var i in fishsArr) {
    if (fishsArr[i]) {
      fishsArr[i].draw();
      fishsArr[i].update();
    }

  }
  for (var i in AnotherFish) {
    if (AnotherFish[i]) {
      AnotherFish[i].draw();
      AnotherFish[i].AnotherFish();
    }
  }
  for (i = 0; i < sprites.length; i++) {
    var a = sprites[i].crashX + 5;
    var b = sprites[i].crashY;
    if (a > canvas.width) {
      sprites[i].speed = -10;
      sprites[i].appNum += 1;
    }
    if (a < 0) {
      sprites[i].speed = -10;
      sprites[i].appNum += 1;
    }
    if (b < 0) {
      sprites[i].speed = -10;
      sprites[i].appNum += 1;
    }
    if (b > canvas.height) {
      sprites[i].speed = 10;
      sprites[i].appNum += 1;
    }
  }
  for (i = 0; i < sprites.length; i++) {
    if (sprites[i].appNum >= 3) {
      console.log(sprites[i].appNum);
      sprites.splice(i, 1);
    }
  }

  ctx.font = "40px Courier New";
  //设置字体填充颜色
  ctx.fillStyle = "white";
  //从坐标点(50,50)开始绘制文字
  ctx.fillText("分数：" + finalScore, 50, 50);
  bottomBar.draw();
  reduceCannon.draw();
  addCannon.draw();
  Cannon.draw(oNum);
  time++;
  window.requestAnimationFrame(testAnmation);
}
